package gorails.com;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.Toast;

public class ViewTrip extends ListActivity {
	private Button pickLine;
	private Button viewTrip;
	private ListView lv;
	//----------------
	private DBAdapter dbAdapter;
	private static final String TABLE_STATION = "station";
	private static final String TABLE_LINE = "line";
	private static final String COLUMN_UPN = "UnionPacificNorth";
	private static final String COLUMN_MDN = "MilwaukeeDistrictNorth";
	private static final String COLUMN_NCS = "NorthCentralService";
	private static final String COLUMN_UPNW = "UnionPacificNorthwest";
	private static final String COLUMN_MDW = "MilwaukeeDistrictWest";
	private static final String COLUMN_UPW = "UnionPacificWest";
	private static final String COLUMN_BNSF = "BNSFRailway";
	private static final String COLUMN_HC = "HeritageCorridor";
	private static final String COLUMN_SWS = "SouthwestService";
	private static final String COLUMN_RID = "RockIslandDistrict";
	private static final String COLUMN_MED = "MetraElectricDistrict";
	private static final String COLUMN_SIZE = "stationSize";
	private static final String TABLE_UPN_OW = "UnionPacificNorth_OW";
	private static final String TABLE_UPN_OS = "UnionPacificNorth_OS";
	private static final String TABLE_UPN_OH = "UnionPacificNorth_OH";
	private static final String TABLE_UPN_IW = "UnionPacificNorth_IW";
	private static final String TABLE_UPN_IS = "UnionPacificNorth_IS";
	private static final String TABLE_UPN_IH = "UnionPacificNorth_IH";
	private ArrayList<TripListViewOption> list;
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lv = getListView();
        lv.setTextFilterEnabled(true);

       

        //list = getLineStations(pos);
        
       /* list = new ArrayList<String>();
        for(int i = 0;i<10;i++){
        	list.add(""+i);
        }*/
        ArrayList<TripListViewOption> list = getTrips();
        if(list.size()==0){
        	TripListViewOption emp = new TripListViewOption();
        	emp.setTitle("Sorry No Trips Available");
        	list.add(emp);
        }
        //Log.d("Trips List", list.toString());
        setListAdapter(new ViewTripAdapter(this, R.layout.view_trip_item, list));

        
    }
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		TripListViewOption o = (TripListViewOption) this.getListAdapter().getItem(position);
		if(!o.getTitle().equalsIgnoreCase("Sorry No Trips Available")){
			Trip.reset();
			TripOption option = TripOption.getInstance();
			option.setArrival(o.getArrival());
			option.setExpress(o.getExpress());
			option.setStops(o.getStops());
			option.setDeparture(o.getDeparture());
			option.setBringTicket(o.needTicket());
			Intent i = new Intent(ViewTrip.this, TripDetails.class);
	        startActivity(i);
		}
		else{
			Toast.makeText(getApplicationContext(), "Sorry please pick a different time.", 5000).show();
		}
		
		
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
	}
	
	
	public ArrayList<TripListViewOption> getTrips(){    	    	

		dbAdapter = new DBAdapter(this);
		SQLiteDatabase db = dbAdapter.getDB();
		ArrayList<TripListViewOption> LineList = new ArrayList<TripListViewOption>();
		

		String column = Trip.getInstance().getLine().replace(" ", "");
		Log.d("ViewTrip", "trip column= "+column);
		String destTable = column;
		int originPos = Trip.getInstance().getStationPosition();
		int destPos = Trip.getInstance().getDestinationPos();
		int day_of_week = Trip.getInstance().getDayOfWeek();
		Log.d("ViewTrip", originPos+" to "+destPos);
		boolean isOutbound = false;
		//this is reverse because it comes from a listview
		if(destPos>originPos){
			//then it's inbound
			destTable+="_I";
		}
		else{
			//its outbound
			destTable+="_O";
			isOutbound = true;
		}
		Log.d("ViewTrip", originPos+" to "+destPos);
		if(isOutbound){
			//if we have an outbound train metra reverses the table.
			originPos = Trip.getInstance().getDestinationPos();
			destPos = Trip.getInstance().getStationPosition();
			//Trip.getInstance().setDestinationPos(originPos);
			//Trip.getInstance().setStationPosition(destPos);
			String oStation = Trip.getInstance().getOrigin();
			String dStation = Trip.getInstance().getDestination();
			//Trip.getInstance().setOrigin(dStation);
			//Trip.getInstance().setDestination(oStation);
			
			Log.d("ViewTrip is outbound", oStation+" to "+dStation);
		}
		//Please note there is no check for a holiday 
		/*
		 * If(holday) then +="H"
		 * else switch the day of the week
		 * 
		 */
		switch(day_of_week){
		case 1:
			destTable+="S";
			Log.d("ViewTrip", "Day of Week: "+day_of_week+ " sat");//sat
			break;
		case 2:
			destTable+="H";
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " sun");//sun
			break;
		case 3:
			destTable+="W";
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " mon");//mon
			break;
		case 4:
			destTable+="W";
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " tues");//tues
			break;
		case 5: destTable+="W";
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " wed");//wed
			break;
		case 6: destTable+="W"; 
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " thurs");//thurs
			break;
		case 7: destTable+="W"; //friday
			Log.d("ViewTrip", "Day of Week: "+day_of_week + " friday");
			break;
		default: Log.d("ViewTrip", "Day of Week: "+day_of_week); break; 
		}
		Log.d("ViewTrip", "desired Table: "+destTable);

		String[] columns = columnHelper(destTable);

		if(Trip.getInstance().getArriveOrDepart()){//-----------------------------------------------------------------
			//arrive by a specfic time
			int hour = Trip.getInstance().getHour();
			String amPm = "";

			boolean inFuture =Trip.getInstance().getIsInFuture();
			Log.d("In Future", "inFuture="+inFuture);
				// if in future we should show a few trips before the depart time and all after.
				if(hour>12){
					//it's pm
					hour-=12;
					amPm = "p";
							
				}
				else if(hour==0){
					//its midnight
					//this is going to be tough because you need to check for 11p and 12a
					hour+=12;					
					amPm = "a";
				}
				else{
					//its am
					amPm= "a";			
				}
				Log.d("ViewTrip======================", "Arrive: hour= "+hour+amPm);

				String query = "SELECT * FROM " + destTable;
				Cursor c = db.rawQuery(query, null);
				
				String regTest =""; 
				String min = Trip.getInstance().getMinuteS();

				//in this version we are only checking arrivals in the same time period except 12 and 1
				if(amPm.equalsIgnoreCase("p")){
					if(hour==12){
						regTest = "((12:[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|(((11|10|[1-9]:[0-5][0-9]a)";//1am - 12pm
					}
					else if(hour==11){
						regTest = "((11:[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|((10|12|[1-9]):[0-5][0-9]p))";//11p
						// 12pm - 11pm
					}
					else if(hour==10){
						regTest = "((10:+[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|((12|[1-9]):[0-5][0-9]p))";//10p
						// 12pm - 10pm
					}
					else if(hour==9){
						regTest = "((9:[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|((12|[1-8]):[0-5][0-9]p))";//9p
						// 12pm - 9pm
					}
					else if(hour<9&&hour!=1){
						regTest = "(("+hour+":[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|((12|[1-"+(hour-1)+"]):[0-5][0-9]p))";//else
						// 2pm - 8pm
					}
					else{
						//hour==1
						regTest = "(("+hour+":[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|(12:[0-5][0-9]p)|((10|11|12|[1-9]):[0-5][0-9]a))";//1p
					}
				}
				else if(amPm.equalsIgnoreCase("a")){
					if(hour==12){
						regTest = "((12:[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]a)|((10|11)|[1-9]):[0-5][0-9]p))";//12a
					}
					else if(hour==11){
						regTest = "((11:[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]a)|((10|[1-9]):[0-5][0-9]a))";//11a
					}
					else if(hour<11&& hour !=1){
						regTest = "(("+hour+":[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]a)|([1-"+(hour-1)+"]):[0-5][0-9]a))";//10a
					}
					else{
						//hour==1
						regTest = "(("+hour+":[0-"+min.charAt(0)+"][0-"+min.charAt(1)+"]p)|(12:[0-5][0-9]a)|((10|11|12|[1-9]):[0-5][0-9]p))";//1am
					}
				}
				Log.d("REGEX FIRST TEST", regTest);
	

				for (int b = 0; b < columns.length; b++) {
					// for every column we look for the time which is closest to the
					// requested time

					TripListViewOption option = new TripListViewOption();

					// Check to make sure the train can get to the destination
					// if not move to the next train
					Log.d("Arrive By Column", "Column "+columns[b]+"("+originPos+","+b+")" );
					if (c.moveToPosition(originPos)) {
						if (c.getString(b) == null) {;
							continue; // move to next column
						} else {
							// column destination is not null, test the origin
							Pattern p = Pattern.compile(regTest,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
							c.moveToPosition(destPos);
							Log.d("REGEX", "testing "+columns[b]+"("+originPos+","+b+"): " + c.getString(b));
							if (c.getString(b) != null) {
								Matcher m = p.matcher(c.getString(b));
								if (!m.find()) {
									// Toast.makeText(mContext,"Please Enter a valid email address.",3000).show();
									// Log.d("Regex failed",
									// "origin="+c.getString(b));
									//Log.d("column continue 2", "b=" + b);
									continue;
								} else {
									// we have a matching time
									Log.d("Regex win", "dest=" + c.getString(b));
									// Start at the origin and..
									// count the number of stops
									// set departure time
									// figure out if trip will be express
									// set times for each stop
									
									//now we find a stop that will work, we grab every stop for the rest of the day/night

									
									
									int i = originPos;
									int stopC = 0;

									//query for the stations that are part of the trip
									String query1 = "SELECT "+column+" FROM station";
									Cursor stations = db.rawQuery(query1, null);
									
									
									c.moveToPosition(destPos);
									option.setArrival(c.getString(b));
									c.moveToPosition(originPos);
									
									String query2 = "SELECT vendor FROM MetraStationExtras WHERE vendor=\""+c.getString(b)+"\"";
									Cursor v = db.rawQuery(query2, null);
									if(!v.moveToFirst()){
										//no vendor
										//Log.d("Station", "has no vendor");
										option.setBringTicket(true);
									}
									else{
										//vendor is present at station
										Log.d("Station", "has vendor "+v.getString(0));
										option.setBringTicket(false);
									}
									v.close();
									option.setDeparture(c.getString(b));
									
									stations.moveToPosition(destPos);
									//TripOption.getInstance().setArrivalStation(stations.getString(0));
									TripOption.getInstance().setArrivalStation(Trip.getInstance().getDestination());
									stations.moveToPosition(originPos);
									//TripOption.getInstance().setDepartStation(stations.getString(0));
									TripOption.getInstance().setArrivalStation(Trip.getInstance().getOrigin());
									option.setTitle(Trip.getInstance().getOrigin()+" Train "+columns[b].substring(1) );
									int spaceCounter = 0;
									while (i < destPos) {
										c.moveToNext();
										stations.moveToNext();
										if(c.getString(b)!=null){
											option.setStops(stations.getString(0), c.getString(b));
											stopC++;
										}
										else{
											spaceCounter++;
										}
										
										//Log.d("ViewTrip.queryElse.column." + b, ""
										//		+ c.getString(b));
										
										i++;
									}			
									if(spaceCounter!=stopC && spaceCounter!=0){
										option.setExpress("Yes");
									}
									else{
										option.setExpress("No");
									}
									stations.close();
									option.setNumOfStops("" + stopC);
									Log.d("Trip Option", option.toString());
									LineList.add(option); // add the option to the
															// list
								}

							} else {
								// dest is null
								// Log.d("ViewTrip",
								// "origin "+columns[b]+" is null");
								//Log.d("column continue 3", "b=" + b);
								continue;
							}

						}
					} else {
						// Log.d("ViewTrip",
						// "cursor cannot move to destination "+destPos);
						//Log.d("column continue", "b=" + b);
						continue;
					}

				}
				c.close();
				db.close();
				return LineList;
			
		}
		else{//--------------------------------------------------------------------------------------
			//depart
			//requestTime break down
			// [#/##]:##[a/p]


			int hour = Trip.getInstance().getHour();
			String amPm = "";
			boolean inFuture =Trip.getInstance().getIsInFuture();
			Log.d("In Future", "inFuture="+inFuture);
			if(inFuture){
				// if in future we should show a few trips before the depart time and all after.
				if(hour>12){
					//it's pm
					
					hour-=12;
					amPm = "p";

					
									
				}
				else if(hour==0){
					//its midnight
					//this is going to be tough because you need to check for 11p and 12a
					hour+=12;
					
					amPm = "a";
				}
				else{
					//its am
					amPm= "a";		
				}
				Log.d("ViewTrip======================", "in Future Depart hour= "+hour+amPm);
				String oppAMPM = "";
				if(amPm.equalsIgnoreCase("p")){
					oppAMPM = "a";
				}
				else if(amPm.equalsIgnoreCase("a")){
					oppAMPM = "p";
				}
				else{
					oppAMPM = "p";
				}
				String query = "SELECT * FROM " + destTable;
				Cursor c = db.rawQuery(query, null);
				String min = ""+Trip.getInstance().getMinuteS();
				char d1 = min.charAt(0);
				char d2 = min.charAt(1);
				String regTest =""; 
				if(hour==12){
					regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1)?[0-9]:[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
				}
				else if(hour==11){
					regTest = "((11:["+d1+"-5]["+d2+"-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
				}
				else if(hour==10){
					regTest = "((1[0-1]:["+d1+"-5]["+d2+"-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
				}
				else if(hour==9){
					regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1[0-1]):[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
				}
				else{
					regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((["+(hour+1)+"-9]|(1[0-1])):[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
				}
				Log.d("REGEX FIRST TEST", regTest);
	
				
				for (int b = 0; b < columns.length; b++) {
					// for every column we look for the time which is closest to the
					// requested time

					TripListViewOption option = new TripListViewOption();
					Log.d("Depart By Column", "Column "+columns[b]+"("+originPos+","+b+")" );
					// Check to make sure the train can get to the destination
					// if not move to the next train
					if (c.moveToPosition(destPos)) {
						if (c.getString(b) == null) {;
							continue; // move to next column
						} else {
							// column destination is not null, test the origin
							Pattern p = Pattern.compile(regTest,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
							c.moveToPosition(originPos);
							Log.d("REGEX", "testing "+columns[b]+"("+originPos+","+b+"): " + c.getString(b));
							if (c.getString(b) != null) {
								Matcher m = p.matcher(c.getString(b));
								if (!m.find()) {
									// Toast.makeText(mContext,"Please Enter a valid email address.",3000).show();
									// Log.d("Regex failed",
									// "origin="+c.getString(b));
									//Log.d("column continue 2", "b=" + b);
									continue;
								} else {
									// we have a matching time
									Log.d("Regex win", "origin=" + c.getString(b));
									// Start at the origin and..
									// count the number of stops
									// set departure time
									// figure out if trip will be express
									// set times for each stop
									
									//now we find a stop that will work, we grab every stop for the rest of the day/night

									
									
									int i = originPos;
									int stopC = 0;

									//query for the stations that are part of the trip
									String query1 = "SELECT "+column+" FROM station";
									Cursor stations = db.rawQuery(query1, null);
									
									
									c.moveToPosition(destPos);
									option.setArrival(c.getString(b));
									c.moveToPosition(originPos);
									
									String query2 = "SELECT vendor FROM MetraStationExtras WHERE vendor=\""+c.getString(b)+"\"";
									Cursor v = db.rawQuery(query2, null);
									if(!v.moveToFirst()){
										//no vendor
										//Log.d("Station", "has no vendor");
										option.setBringTicket(true);
									}
									else{
										//vendor is present at station
										Log.d("Station", "has vendor "+v.getString(0));
										option.setBringTicket(false);
									}
									v.close();
									option.setDeparture(c.getString(b));
									
									stations.moveToPosition(destPos);
									//TripOption.getInstance().setArrivalStation(stations.getString(0));
									TripOption.getInstance().setArrivalStation(Trip.getInstance().getDestination());
									stations.moveToPosition(originPos);
									//TripOption.getInstance().setDepartStation(stations.getString(0));
									TripOption.getInstance().setArrivalStation(Trip.getInstance().getOrigin());
									option.setTitle(Trip.getInstance().getOrigin()+" Train "+columns[b].substring(1) );
									int spaceCounter = 0;
									while (i < destPos) {
										c.moveToNext();
										stations.moveToNext();
										if(c.getString(b)!=null){
											option.setStops(stations.getString(0), c.getString(b));
											stopC++;
										}
										else{
											spaceCounter++;
										}
										
										//Log.d("ViewTrip.queryElse.column." + b, ""
										//		+ c.getString(b));
										
										i++;
									}			
									if(spaceCounter!=stopC && spaceCounter!=0){
										option.setExpress("Yes");
									}
									else{
										option.setExpress("No");
									}
									stations.close();
									option.setNumOfStops("" + stopC);
									//Log.d("Trip Option", option.toString());
									LineList.add(option); // add the option to the
															// list
								}

							} else {
								// origin is null
								// Log.d("ViewTrip",
								// "origin "+columns[b]+" is null");
								//Log.d("column continue 3", "b=" + b);
								continue;
							}

						}
					} else {
						// Log.d("ViewTrip",
						// "cursor cannot move to destination "+destPos);
						//Log.d("column continue", "b=" + b);
						continue;
					}

				}
				c.close();
				db.close();
				return LineList;
			}
			else{ //--------------------------------Current Time 
				//show only trips
				if(hour>12){
					//it's pm
					
					hour-=12;
					amPm = "p";				
				}
				else if(hour==0){
					//its midnight
					//this is going to be tough because you need to check for 11p and 12a
					hour+=12;
					amPm = "a";
				}
				else{
					//its am
					amPm= "a";						
				}
				Log.d("ViewTrip======================", "Depart Current Time: hour= "+hour+amPm);

				String query = "SELECT * FROM " + destTable;
				Cursor c = db.rawQuery(query, null);
				String oppAMPM = "";
				if(amPm.equalsIgnoreCase("p")){
					oppAMPM = "a";
				}
				else if(amPm.equalsIgnoreCase("a")){
					oppAMPM = "p";
				}
				else{
					oppAMPM = "p";
				}
				String min = ""+Trip.getInstance().getMinuteS();
				char d1 = min.charAt(0);
				char d2 = min.charAt(1);
				String regTest =""; 
				if(amPm.equalsIgnoreCase("a")){
					if(hour==12){
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1)?[0-9]:[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
					}
					else if(hour==11){
						regTest = "((11:["+d1+"-5]["+d2+"-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
					}
					else if(hour==10){
						regTest = "((1[0-1]:["+d1+"-5]["+d2+"-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
					}
					else if(hour==9){
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1[0-1]):[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
					}
					else{
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((["+(hour+1)+"-9]|(1[0-1])):[0-5][0-9]"+amPm+")|(([1-9]|(10|11|12)):[0-5][0-9]"+oppAMPM+"))";
					}
				}
				else{
					//its pm
					if(hour==12){
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1)?[0-9]:[0-5][0-9]"+amPm+"))";
					}
					else if(hour==11){
						regTest = "((11:["+d1+"-5]["+d2+"-9]"+amPm+"))";
					}
					else if(hour==10){
						regTest = "((1[0-1]:["+d1+"-5]["+d2+"-9]"+amPm+"))";
					}
					else if(hour==9){
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((1[0-1]):[0-5][0-9]"+amPm+"))";
					}
					else{
						regTest = "(("+hour+":["+d1+"-5]["+d2+"-9]"+amPm+")|((["+(hour+1)+"-9]|(1[0-1])):[0-5][0-9]"+amPm+"))";
					}
				}
				
				Log.d("REGEX FIRST TEST", regTest);

				for (int b = 0; b < columns.length; b++) {
					// for every column we look for the time which is closest to the
					// requested time

					TripListViewOption option = new TripListViewOption();
					Log.d("Depart By Column current time", "Column "+columns[b]+"("+originPos+","+b+")" );
					// Check to make sure the train can get to the destination
					// if not move to the next train
					if (c.moveToPosition(destPos)) {
						if (c.getString(b) == null) {;
							continue; // move to next column
						} else {
							// column destination is not null, test the origin
							Pattern p = Pattern.compile(regTest,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
							c.moveToPosition(originPos);
							Log.d("REGEX", "testing "+columns[b]+"("+originPos+","+b+"): " + c.getString(b));
							if (c.getString(b) != null) {
								Matcher m = p.matcher(c.getString(b));
								if (!m.find()) {
									continue;
								} else {
									// we have a matching time
									Log.d("Regex win", "origin=" + c.getString(b));
									// Start at the origin and..
									// count the number of stops
									// set departure time
									// figure out if trip will be express
									// set times for each stop
									

									
									int i = originPos;
									int stopC = 0;

									//query for the stations that are part of the trip
									String query1 = "SELECT "+column+" FROM station";
									Cursor stations = db.rawQuery(query1, null);
									
									
									c.moveToPosition(destPos);
									option.setArrival(c.getString(b));
									c.moveToPosition(originPos);
									
									String query2 = "SELECT vendor FROM MetraStationExtras WHERE vendor=\""+c.getString(b)+"\"";
									Cursor v = db.rawQuery(query2, null);
									if(!v.moveToFirst()){
										//no vendor
										//Log.d("Station", "has no vendor");
										option.setBringTicket(true);
									}
									else{
										//vendor is present at station
										Log.d("Station", "has vendor "+v.getString(0));
										option.setBringTicket(false);
									}
									v.close();
									option.setDeparture(c.getString(b));
									
									stations.moveToPosition(destPos);
									//TripOption.getInstance().setArrivalStation(stations.getString(0));
									TripOption.getInstance().setArrivalStation(Trip.getInstance().getDestination());
									stations.moveToPosition(originPos);
									//TripOption.getInstance().setDepartStation(stations.getString(0));
									TripOption.getInstance().setDepartStation(Trip.getInstance().getOrigin());
									option.setTitle(Trip.getInstance().getOrigin()+" Train "+columns[b].substring(1) );
									int spaceCounter=0;
									while (i < destPos) {
										c.moveToNext();
										stations.moveToNext();
										if(c.getString(b)!=null){
											option.setStops(stations.getString(0), c.getString(b));
											stopC++;
										}
										else{
											spaceCounter++;
										}
										
										//Log.d("ViewTrip.queryElse.column." + b, ""
										//		+ c.getString(b));
										i++;
									}								
									stations.close();
									if(spaceCounter!=stopC && spaceCounter!=0){
										option.setExpress("Yes");
									}
									else{
										option.setExpress("No");
									}
									option.setNumOfStops("" + stopC);
									//Log.d("Trip Option", option.toString());
									LineList.add(option); // add the option to the
															// list
								}

							} else {
								// origin is null
								// Log.d("ViewTrip",
								// "origin "+columns[b]+" is null");
								//Log.d("column continue 3", "b=" + b);
								continue;
							}

						}
					} else {
						// Log.d("ViewTrip",
						// "cursor cannot move to destination "+destPos);
						//Log.d("column continue", "b=" + b);
						continue;
					}

				}
				c.close();
				db.close();
				return LineList;
			}
			
			
			
			
		}		 	
	}
	
	private String[] columnHelper(String table){

		if(table.equalsIgnoreCase(TABLE_UPN_OW)){
			int[] UPN_OW_Trains = UPN_Trains.UPN_OW_Trains;
			String[] UPN_OW_Columns = new String[UPN_OW_Trains.length];
			int currentTrain = 301;
	    	for(int i = 0;i<UPN_OW_Trains.length;i++){
	    		UPN_OW_Trains[i]=currentTrain;
	    		currentTrain+=2;
	    	}			
	    	for(int index = 0; index < UPN_OW_Columns.length; index++){
      	    	UPN_OW_Columns[index]="t"+UPN_OW_Trains[index];    			
			}
			return UPN_OW_Columns;
		}
		else if(table.equalsIgnoreCase(TABLE_UPN_IW)){
			Log.d("Column Helper", "UPN_IW");
			int[] UPN_IW_Trains = UPN_Trains.UPN_IW_Trains;
			String[] UPN_IW_Columns = new String[UPN_IW_Trains.length];			
			int currentTrain = 300;
      	    for(int i = 0;i<UPN_IW_Trains.length;i++){    		
      	    	UPN_IW_Trains[i]=currentTrain;
      	    	currentTrain+=2;
      	    }
			for(int index = 0; index < UPN_IW_Columns.length; index++){
      	    	UPN_IW_Columns[index]="t"+UPN_IW_Trains[index];    			
			}
			return UPN_IW_Columns;
		}
		else if(table.equalsIgnoreCase(TABLE_UPN_IS)){			
			int[] UPN_IS_Trains = new int[]{302,800,802,804,806,334,336,338,342,810,812,814,818};
			String[] UPN_IS_Columns = new String[UPN_IS_Trains.length];
			for(int i =0;i<UPN_IS_Trains.length;i++){
				UPN_IS_Columns[i]="t"+UPN_IS_Trains[i];
	    	}
			return UPN_IS_Columns;
		}
		else if(table.equalsIgnoreCase(TABLE_UPN_OS)){			
			int[] UPN_OS_Trains = UPN_Trains.UPN_OS_Trains;
			String[] UPN_OS_Columns = new String[UPN_OS_Trains.length];			
			for(int i =0;i<UPN_OS_Trains.length;i++){
				UPN_OS_Columns[i]="t"+UPN_OS_Trains[i];
				Log.d("column helper", "column "+UPN_OS_Columns[i]);
	    	}
			return UPN_OS_Columns;
		}
		else if(table.equalsIgnoreCase(TABLE_UPN_IH)){
			int[] UPN_IH_Trains = new int[]{802,334,808,338,342,810,812,814,816};
			String[] UPN_IH_Columns = new String[UPN_IH_Trains.length];
			for(int i =0;i<UPN_IH_Trains.length;i++){
				UPN_IH_Columns[i]="t"+UPN_IH_Trains[i];
	    	}
			return UPN_IH_Columns;
		}
		else if(table.equalsIgnoreCase(TABLE_UPN_OH)){
			int[] UPN_OH_Trains = new int[]{301,321,803,805,807,809,111,815,363,367};
			String[] UPN_OH_Columns = new String[UPN_OH_Trains.length];
			for(int i =0;i<UPN_OH_Trains.length;i++){
				UPN_OH_Columns[i]="t"+UPN_OH_Trains[i];
	    	}
			return UPN_OH_Columns;
		}
		else{
			Toast.makeText(getApplicationContext(), "Line not created yet...", 5000);
			
			return null;
		}

	}

	
}
